﻿<%@ WebHandler Language="C#" Class="ProductProcessSampleHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Data;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using BusinessRule.DTO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using DatabaseAccess.DBUtility;
using Camstar.WebClient.FormsBuilder.WebForm;
using Camstar.WebClient.WebServicesProxy.InSiteWebServices;
using External.APICalling;

public class ProductProcessSampleHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetProductProcessSample"://查询产品加工试样信息
                    GetProductProcessSample(context);
                    break;
                case "SaveProductProcessSample"://
                    SaveProductProcessSample(context);
                    break;
                case "DeleteProductProcessSample"://
                    DeleteProductProcessSample(context);
                    break;
                case "GetWorkflowStepData":
                    GetWorkflowStepData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


    void GetProductProcessSample(HttpContext context)
    {
        var paraData = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var selectSQL = @"SELECT p.*,e.FULLNAME CreateNAME,e2.FULLNAME UPDATENAme FROM ProductProcessSample p 
                        LEFT JOIN EMPLOYEE e ON e.EMPLOYEEID =p.CREATEEMPID 
                        LEFT JOIN EMPLOYEE e2 ON e2.EMPLOYEEID =p.UPDATEEMPID 
                        WHERE 1=1";
        if (!string.IsNullOrWhiteSpace(paraData["ProductName"]))
        {
            selectSQL += " and p.ProductName like '%" + paraData["ProductName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["WorkflowStepName"]))
        {
            selectSQL += " and p.WorkflowStepName like '%" + paraData["WorkflowStepName"] + "%'";
        }
        selectSQL += "order by p.CREATEDATE desc ";
        var paraPage = new uMESPagingDataDTO();
        paraPage.CurrentPageIndex = Convert.ToInt32(pageNumber);
        paraPage.PageSize = Convert.ToInt32(pageSize);
        paraPage.strSQL = selectSQL;
        var result = DatabaseHelper.RunProcedureForPage(paraPage);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    void SaveProductProcessSample(HttpContext context)
    {
        var paraData = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        var insertSQL = @"INSERT INTO PRODUCTPROCESSSAMPLE
                        (PRODUCTPROCESSSAMPLEID, PRODUCTNAME, WORKFLOWSTEPNAME, SAMPLESPECIFICATION, SAMPLEMATERIAL, SAMPLESTATUS, SAMPLEQTY, NOTES, CREATEDATE, CREATEEMPID)
                        VALUES(sys_guid(), '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', sysdate,'{7}')";
        var updateSQL = @"UPDATE PRODUCTPROCESSSAMPLE
                        SET PRODUCTNAME='{0}', WORKFLOWSTEPNAME='{1}', SAMPLESPECIFICATION='{2}', SAMPLEMATERIAL='{3}', SAMPLESTATUS='{4}', SAMPLEQTY='{5}', NOTES='{6}', UPDATEDATE=SYSDATE, UPDATEEMPID='{7}'
                        WHERE PRODUCTPROCESSSAMPLEID='{8}'";
        var selectSQL = @"select * from PRODUCTPROCESSSAMPLE p where p.PRODUCTNAME='{0}' and p.WORKFLOWSTEPNAME='{1}'";
        var sql = "";
        var result = new uMESProcessResult();
        if (!string.IsNullOrWhiteSpace(paraData["ProductProcessSampleId"]))
        {
            sql = string.Format(updateSQL, paraData["ProductName"].Split(':')[0], paraData["WorkflowStepName"], paraData["SampleSpecification"], paraData["SampleMaterial"], paraData["SampleStatus"], paraData["SampleQty"], paraData["Notes"], m_UserInfo.EmployeeID, paraData["ProductProcessSampleId"]);
        }
        else
        {
            var dt = DatabaseHelper.Query_ReturnDatTable(string.Format(selectSQL, paraData["ProductName"].Split(':')[0], paraData["WorkflowStepName"]));
            if (dt.Rows.Count == 0)
            {
                sql = string.Format(insertSQL, paraData["ProductName"].Split(':')[0], paraData["WorkflowStepName"], paraData["SampleSpecification"], paraData["SampleMaterial"], paraData["SampleStatus"], paraData["SampleQty"], paraData["Notes"], m_UserInfo.EmployeeID);
            }
            else
            {
                result.Message = "件号工序数据已维护";
            }
        }
        if (!string.IsNullOrWhiteSpace(sql))
        {
            result.Result = DatabaseHelper.ExecuteSql(sql) > 0;
            result.Message = result.Result ? "操作成功" : "操作失败";
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    void DeleteProductProcessSample(HttpContext context)
    {
        var ids = context.Request["ids"].ToString();
        var result = new uMESProcessResult();
        var sql = @"DELETE FROM PRODUCTPROCESSSAMPLE WHERE PRODUCTPROCESSSAMPLEID IN ('{0}')";
        result.Result = DatabaseHelper.ExecuteSql(string.Format(sql, ids.Replace(",", "','"))) > 0;
        result.Message = result.Result ? "删除成功" : "删除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    void GetWorkflowStepData(HttpContext context)
    {
        var productid = context.Request["productid"].ToString();
        var result = new uMESProcessResult();
        var sql = @"SELECT ws.WORKFLOWSTEPNAME FROM PRODUCT p
                LEFT JOIN PRODUCTBASE pb ON pb.PRODUCTBASEID =p.PRODUCTBASEID 
                LEFT JOIN WORKFLOWBASE WFB ON WFB.WORKFLOWBASEID = P.WORKFLOWBASEID
                LEFT JOIN WORKFLOW w ON w.WORKFLOWID =NVL(WFB.REVOFRCDID, P.WORKFLOWID)
                LEFT JOIN WORKFLOWSTEP ws ON ws.WORKFLOWID =w.WORKFLOWID 
                WHERE  p.PRODUCTID ='{0}'";
        result.DBTable = DatabaseHelper.Query_ReturnDatTable(string.Format(sql, productid));
        result.Result = result.DBTable.Rows.Count > 0;
        result.Message = result.Result ? "" : "件号无工艺工序信息";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

}